package com.heartOfGold;

/*  A class to test the serial communications for the heart of
 *  gold utilizing the Observer Observable Design pattern
 *
 *  @author by Ryan T. Higgins
 *  @date 12Apr04
 */

import com.heartOfGold.model.Message;
import com.heartOfGold.model.megasquirtECU.MegasquirtMessenger;

import java.io.*;
import java.util.Observable;
import java.util.Observer;
import java.util.Iterator;





public class TestMain implements Observer {
    int num = 0;

    /* (non-Javadoc)
     * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
     */
    public final void update(final Observable o, final Object arg) {
        Message message = (Message) arg;
        String args = message.getID();
        Iterator<Object> stuff = message.getArgs();
        if (!args.equals("fuel")) {
            while (stuff.hasNext()) {
                args += ",";
                args += (String) stuff.next();
            }
            System.out.println("This is the message received from"
                    + " Observable HogMessenger - " + args);
        } else {
            byte[] bytes = null;
            int numBytes = 0;
            int temp = 0;
            String ints = new String();
            if (stuff.hasNext()) {
                bytes = (byte[]) stuff.next();
            }
            if (stuff.hasNext()) {
                numBytes = ((Integer) stuff.next()).intValue();
            }
            if (bytes != null) {
                for (int i = 0; i < numBytes; i++) {
                    temp = (int) bytes[i];
                    if (temp < 0) {
                        temp += 256;
                    }
                    ints += "Value " + num + " = ";
                    ints += temp;
                    ints += '\n';
                    num++;
                }
            }
            System.out.println("And here's a Mega-message with"
                    + " the bytes cast as ints:\n" + ints);
        }
        return;
    }

    /**
     * @param args
     * @throws java.io.IOException
     */
    public static void main(final String[] args) throws java.io.IOException {
        // int numRead;
        // char fromCommand[];
        String[] words;
        byte[] bytes;

        MegasquirtMessenger thinger = MegasquirtMessenger.getMessenger(args[0], args[1]);
        TestMain theMain = new TestMain();
        System.out.println("starting messaging");
        thinger.startMessaging();
        System.out.println("messaging started");
        thinger.addObserver(theMain);
        System.out.println("Added self as observer");
        BufferedReader console = new BufferedReader(new InputStreamReader(
                System.in));
        for (;;) {
            System.out.println("Enter a comma delimited message containing"
                    + " neither a $ or an *, and message ID as first "
                    + "the  word to send!");
            String stringMessage = console.readLine();
            Message theMessage = new Message();
            words = stringMessage.split(",");
            theMessage.setID(words[0]);
            if (!(words[0]).equals("fuel")) {
                for (int i = 1; i < words.length; ++i) {
                    theMessage.addArg(words[i]);
                }
            } else {
                bytes = new byte[(words.length - 1)];
                for (int i = 1; i < words.length; ++i) {
                    bytes[i - 1] = (byte) (words[i]).charAt(0);
                }
                theMessage.addArg(bytes);
                theMessage.addArg(new Integer(words.length - 1));
            }
            thinger.sendMessage(theMessage);
        }
    }
}
